package com.ft.f_springboot_route.controller;


import com.ft.f_springboot_route.bean.Category;
import com.ft.f_springboot_route.service.CategoryService;
import com.ft.f_springboot_route.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    /***
     * 获取所有的分类并回显到分类首页的方法
     * @param currentPage 页码
     * @param cateName 分类名称
     * @param pagersNumber 分页的数量
     * @param model
     */
    @RequestMapping("/toCategoryList")
    public String toCategoryList(Integer currentPage, String cateName,Integer pagersNumber ,Model model){
        Integer yema = 1;
        if (currentPage !=null){
            yema = currentPage;
        }
        model.addAttribute("currentPage",yema);
        String cateName2 = "";
        if (cateName != null){
            cateName2 = cateName;

        }
        model.addAttribute("cateName",cateName);
        Integer pagersNumber2 = 4;
        if(pagersNumber != null){
            pagersNumber2 = pagersNumber;
        }
        model.addAttribute("pagersNumber2",pagersNumber2);
        Pager<Category> categoryList = categoryService.selectAllCategoryList(cateName2,yema,pagersNumber2);
        List<Category> list = categoryList.getList();
        for (Category category : list) {
            System.out.println(category.toString());
        }
        model.addAttribute("categoryList",categoryList);
        return "category/categorylist";
    }

    /***
     * 转到category的添加页面的方法
     * @return
     */
    @RequestMapping("/toCategoryAdd")
    public String toCategoryAdd(){
        return "category/addcategory";
    }

    /***
     * 添加category分类的方法
     * @param cateName 分类名称
     * @param model
     */
    @RequestMapping("/addCategory")
    public String addCategory(String cateName,Model model){
        categoryService.addCategory(cateName);
        return "redirect:/category/toCategoryList";
    }

    /***
     * 转到分类修改的方法
     * @param cateId 分类id
     * @param model
     * @return
     */
    @RequestMapping("/toCategoryEdit")
    public String toCategoryEdit(Integer cateId,Model model){
        Category category = categoryService.selectCategoryByCateId(cateId);
        model.addAttribute("category",category);
        return "category/setcategory";
    }

    /***
     * 获取前台修改的东西
     * 修改分类的方法
     * @param category 获取前台分类存成一个集合
     * @param model
     */
    @RequestMapping("saveCategory")
    public String saveCategory(Category category,Model model){
        categoryService.editCategoryByCategory(category);
        return "redirect:/category/toCategoryList";
    }
    /***
     * 单纯的测试方法
     */
    @RequestMapping("/deletecategory")
    public String deletecategory(Integer cateId,Model model){
        categoryService.deletecategoryBycateId(cateId);
        return "redirect:/category/toCategoryList";
    }
}
